/*
 * This file is part of Cleanflight.
 *
 * Cleanflight is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Cleanflight is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Cleanflight.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdbool.h>
#include <stdint.h>

#include "display.h"
#include "max7456.h"
#include "osd_slave.h"

displayPort_t max7456DisplayPort;

displayPortProfile_t    *displayPortProfileMax7456;


void pgResetFn_displayPortProfileMax7456(displayPortProfile_t *displayPortProfile)
{
    displayPortProfile->colAdjust = 0;
    displayPortProfile->rowAdjust = 0;

    // Set defaults as per MAX7456 datasheet
    displayPortProfile->invert = false;
    displayPortProfile->blackBrightness = 0;
    displayPortProfile->whiteBrightness = 2;
}

static int grab(displayPort_t *displayPort)
{
    // FIXME this should probably not have a dependency on the OSD or OSD slave code
    // osdResetAlarms();
    return 0;
}

static int release(displayPort_t *displayPort)
{

    return 0;
}

static int clearScreen(displayPort_t *displayPort)
{

    max7456Invert(displayPortProfileMax7456->invert);
    max7456Brightness(displayPortProfileMax7456->blackBrightness, displayPortProfileMax7456->whiteBrightness);

    max7456ClearScreen();

    return 0;
}

static int drawScreen(displayPort_t *displayPort)
{
    max7456DrawScreen();

    return 0;
}

static int screenSize(const displayPort_t *displayPort)
{
    return maxScreenSize;
}

static int writeString(displayPort_t *displayPort, uint8_t x, uint8_t y, const char *s)
{
    max7456Write(x, y, s);

    return 0;
}

static int writeChar(displayPort_t *displayPort, uint8_t x, uint8_t y, uint8_t c)
{
    max7456WriteChar(x, y, c);

    return 0;
}

static bool isTransferInProgress(const displayPort_t *displayPort)
{
    return max7456DmaInProgress();
}

static void resync(displayPort_t *displayPort)
{
    max7456RefreshAll();
    displayPort->rows = max7456GetRowsCount() + displayPortProfileMax7456->rowAdjust;
    displayPort->cols = 30 + displayPortProfileMax7456->colAdjust;
}

static void reInit(displayPort_t *displayPort)
{
    max7456ReInit();
}

static int heartbeat(displayPort_t *displayPort)
{
    return 0;
}

static uint32_t txBytesFree(const displayPort_t *displayPort)
{
    return UINT32_MAX;
}

static const displayPortVTable_t max7456VTable = {
    .grab = grab,
    .release = release,
    .clearScreen = clearScreen,
    .drawScreen = drawScreen,
    .screenSize = screenSize,
    .writeString = writeString,
    .writeChar = writeChar,
    .isTransferInProgress = isTransferInProgress,
    .heartbeat = heartbeat,
    .resync = resync,
    .txBytesFree = txBytesFree,
    .reInit = reInit,
};


displayPort_t *max7456DisplayPortInit(const vcdProfile_t *vcdProfile)
{
    // 框架初始化
    pgResetFn_displayPortProfileMax7456(displayPortProfileMax7456);
    // 框架初始化
    displayInit(&max7456DisplayPort, &max7456VTable);
    // 硬件初始化
    max7456Init(vcdProfile);
    // 刷新一次屏幕
    resync(&max7456DisplayPort);
    return &max7456DisplayPort;
}





